Page({
	data: {
	  isLogin: false,
	  userInfo: null
	},
  
	onLoad() {
	  this.checkLoginStatus();
	},
  

	checkLoginStatus() {
	  const userData = wx.getStorageSync('localUserData');
	  if (userData) {
		this.setData({
		  isLogin: true,
		  userInfo: userData
		});
	  }
	},
  

	handleLogin() {
	  wx.showModal({
		title: '提示',
		content: '获取微信头像和昵称',
		confirmText: '是',
		cancelText: '否',
		success: (res) => {
		  if (res.confirm) {
			this.getUserProfile();
		  }
		}
		
	  });
	},
  

	getUserProfile() {
	  wx.getUserProfile({
		desc: '123456',
		lang: 'zh_CN',
		success: res => {
		  this.saveUserData({
			avatar: res.userInfo.avatarUrl,
			nickname: res.userInfo.nickName,

		  });
		},
		fail: err => {
		  console.error('获取失败', err);
		  wx.showToast({
			title: '失败',
			icon: 'none'
		  });
		}
	  });
	},
  

	saveUserData(data) {
	  wx.setStorageSync('localUserData', data);
	  this.setData({
		isLogin: true,
		userInfo: data
	  });
	  wx.showToast({
		title: '成功',
		icon: 'success'
	  });
	},
  

	handleLogout() {
	  wx.showModal({
		title: '退出',
		content: '退出后重登',
		success: res => {
		  if (res.confirm) {
			wx.removeStorageSync('localUserData');
			this.setData({
			  isLogin: false,
			  userInfo: null
			});
		  }
		}
	  });
	},
  
  });